package com.hliushi.redis.controller;

import com.hliushi.redis.config.Constants;
import com.hliushi.redis.controller.vo.PostUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author llhuang10
 * @date 2021/8/14 15:37
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class LikeController {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 点赞
     *
     * @param vo 帖子id, 用户id
     * @return
     */
    @PostMapping("/do-like")
    public ResponseEntity<String> doLike(@RequestBody PostUserVO vo) {
        String result = "";

        try {
            long num = redisTemplate.opsForSet().add(Constants.LIKE_KEY + vo.getPostId(), vo.getUserId());
            if (num == 1) {
                result = "点赞成功";
            } else {
                result = "你已重复点赞";
            }
            log.info("查询结果: {}", num);
        } catch (Exception e) {
            log.error("exception:", e);
        }

        return ResponseEntity.ok(result);
    }


    /**
     * 取消点赞
     *
     * @param vo 帖子id, 用户id
     * @return
     */
    @PutMapping("/undo-like")
    public ResponseEntity<String> undoLike(@RequestBody PostUserVO vo) {
        String result = "";

        try {
            long num = redisTemplate.opsForSet().remove(Constants.LIKE_KEY + vo.getPostId(), vo.getUserId());
            if (num == 1) {
                result = "取消成功";
            } else {
                result = "你已重复取消点赞";
            }
            log.info("查询结果: {}", num);
        } catch (Exception e) {
            log.error("exception:", e);
        }

        return ResponseEntity.ok(result);
    }


    /**
     * 根据 postId 和userId 查看该帖子信息, 返回结果是点赞总数和是否点赞
     */
    @GetMapping("/get-post/{postId}/{userId}")
    public ResponseEntity<Map<String, Long>> getPost(@PathVariable String postId, @PathVariable String userId) {
        Map<String, Long> map = new HashMap<>(16);

        String key = Constants.LIKE_KEY + postId;
        try {
            Long size = redisTemplate.opsForSet().size(key);
            boolean bool = Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(key, userId));
            if (bool) {
                // 1代表赞
                map.put("isLike", 1L);
            } else {
                // 0代表没有点赞
                map.put("isLike", 0L);
            }
            map.put("size", size);

            log.info("查询结果：{}", map);
        } catch (Exception e) {
            log.error("exception:", e);
        }

        return ResponseEntity.ok(map);
    }

    /**
     * 查看点赞明细, 就是有哪些人点赞
     */
    @GetMapping("post-detail")
    public ResponseEntity<Set<String>> likeDetail(Integer postId) {
        Set<String> members = new HashSet<>();
        if (postId == null) {
            throw new NullPointerException("输入的帖子postId为空!");
        }
        try {
            members = redisTemplate.opsForSet().members(Constants.LIKE_KEY + postId);

            log.info("查询结果: {}", members);
        } catch (Exception e) {
            log.error("exception:", e);
        }


        return ResponseEntity.ok(members);

    }
}
